import moment from "moment/moment";

export const showToast = async (self, options) => {
	await self.$refs.zkView.showToast(options);
}

export const showModal = (self, options) => {
	return self.$refs.zkView.showModal(options);
}

export const checkRequired = (options) => {
	console.log(options);
	let checkRes = true;
	if (options && options.length > 0) {
		for (let i = 0; i < options.length; i++) {
			const opt = options[i];
			if (typeof opt === 'string' && !opt) {
				checkRes = false;
			}
			if (typeof opt === 'number' && opt < 0) {
				checkRes = false;
			}
		}
		if (!checkRes) {
			uni.showToast({
				title: "缺少必填入参！！！",
				icon: "loading"
			});
		}
	}
	return checkRes;
}

export const imageErr = e => {
	console.warn('图片加载失败！' + e.detail.errMsg);
	return;
}

export const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()
	return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

export const currentTime = (formatStr) => {
	formatStr = formatStr || 'YYYY-MM-DD HH:mm:ss';
	return moment().format(formatStr);
}